﻿namespace PowerSong.SongDatabase.ObjectModel
    {
    using System;
    using System.Collections.Generic;

    public class Settings : ObjectModelBase
        {

        private Configuration configuration = null;

        internal Settings(Database database, Configuration configuration)
            : base(database) 
            {
            this.configuration = configuration;
            }

        public bool Exists(string name)
            {
            return configuration.Values.ContainsKey(name);
            }

        public string Get(string name)
            {
            if (configuration.Values.ContainsKey(name))
                {
                return configuration.Values[name];
                }
            else
                {
                return null;
                }
            }

        public T Get<T>(string name)
            {
            return Get<T>(name, default(T));
            }

        public T Get<T>(string name, T defaultValue)
            {
            if (configuration.Values.ContainsKey(name))
                {
                var value = configuration.Values[name];
                return (T)Convert.ChangeType(value, typeof(T));
                }
            else
                {
                return defaultValue;
                }
            }

        public Guid GetGuid(string name)
            {
            var value = Get(name);
            return value == null ? Guid.Empty : new Guid(value);
            }

        public void Set(string name, string value)
            {
            if (value == null)
                {
                if (configuration.Values.ContainsKey(name)) configuration.Values.Remove(name);
                }
            else
                {
                configuration.Values[name] = value;
                }
            }

        public void Set(string name, double value)
            {
            configuration.Values[name] = value.ToString();
            }

        public void Set(string name, float value)
            {
            configuration.Values[name] = value.ToString();
            }

        public void Set(string name, int value)
            {
            configuration.Values[name] = value.ToString();
            }

        public void Set(string name, uint value)
            {
            configuration.Values[name] = value.ToString();
            }

        public void Set(string name, bool value)
            {
            configuration.Values[name] = value.ToString();
            }

        public void Set(string name, Guid value)
            {
            configuration.Values[name] = value.ToString();
            }

        public void Delete(string name)
            {
            if (configuration.Values.ContainsKey(name))
                {
                configuration.Values.Remove(name);
                }
            }

        /// <summary>
        /// Saves all changes made to the settings since they were loaded, or previously saved.
        /// Call this method after making one or more changes, in order to have those changes persisted in the database.
        /// </summary>
        public void SaveChanges()
            {
            Database.Write(configuration);
            }

        }

    }
